package com.mangatta.ec2.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.mangatta.ec2.adaptor.InstanceAlertAdaptor;
import com.mangatta.ec2.util.InstanceAlertMap;
import com.mangatta.ec2.util.Util;

import java.util.ArrayList;
import java.util.List;

public class AlertNotificationActivity extends ListActivity {
    private final static String TAG = AlertNotificationActivity.class.getSimpleName();
    private List<InstanceAlertMap> instanceAlertMaps;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_notification_list);

        Intent intent = getIntent();
        ArrayList<String> instanceIds = intent.getStringArrayListExtra("InstanceIds");
        ArrayList<String> metrics = intent.getStringArrayListExtra("Metrics");

        instanceAlertMaps = new ArrayList<InstanceAlertMap>();
        if (instanceIds != null && metrics != null) {
            for (int i = 0; i < Math.min(instanceIds.size(), metrics.size()); i++) {
                instanceAlertMaps.add(new InstanceAlertMap(instanceIds.get(i), metrics.get(i)));
            }
        }

        ListAdapter adapter = new InstanceAlertAdaptor(this, instanceAlertMaps, android.R.layout.simple_list_item_2,
                new String[]{InstanceAlertMap.KEY_INSTANCE_ID, InstanceAlertMap.KEY_ALERT_METRICS},
                new int[]{android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
        Log.d(TAG, "onCreated");
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (instanceAlertMaps != null && instanceAlertMaps.size() > position) {
            InstanceAlertMap instanceAlertMap = instanceAlertMaps.get(position);
            String instanceId = instanceAlertMap.get(InstanceAlertMap.KEY_INSTANCE_ID);
            Intent intent = new Intent(this, AlarmConfigurationListActivity.class);
            intent.putExtra(Util.INSTANCE_ID, instanceId);
            startActivity(intent);
        }
    }
}
